package com.xzm.project.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ListView;

import com.xzm.project.R;
import com.xzm.project.ui.adapter.ListViewAdapter;
import com.xzm.project.ui.adapter.ViewPagerAdapter;
import com.xzm.project.ui.widget.BottomScrollView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;

/**
 * 参考
 * https://github.com/Kyogirante/SvSlideDemo/tree/master
 */
public class NestActivity extends AppCompatActivity {
    /**
     * 下拉刷新控件
     */
    private SwipeRefreshLayout mRefreshLayout;
    private BottomScrollView mScrollView;
    private ViewPager mViewPager;
    private ListView mListView;

    private ViewPagerAdapter mViewPagerAdapter;
    private ListViewAdapter mListViewAdapter;
    //ScrollView是否滚动到底部标志
    private boolean isSvToBottom = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nest);
        initView();
        initListener();
        fixSlideConflict();
    }


    private void initView() {
        mRefreshLayout = findViewById(R.id.main_refresh_layout);
        mScrollView = findViewById(R.id.main_scroll_view);
        mViewPager = findViewById(R.id.main_viewpager);
        mListView = findViewById(R.id.main_list_view);

        mListViewAdapter = new ListViewAdapter(this, getListViewData());
        mListView.setAdapter(mListViewAdapter);

        mViewPagerAdapter = new ViewPagerAdapter(this, getViewPagerData());
        mViewPager.setAdapter(mViewPagerAdapter);
    }

    private void initListener() {
        mRefreshLayout.setOnRefreshListener(() -> mRefreshLayout.postDelayed(() -> mRefreshLayout.setRefreshing(false), 500));
        mScrollView.setOnScrollToBottomListener(new BottomScrollView.OnScrollToBottomListener() {
            @Override
            public void onScrollToBottom() {
                isSvToBottom = true;
            }

            @Override
            public void onNotScrollToBottom() {
                isSvToBottom = false;
            }
        });
    }

    private float mLastX;
    private float mLastY;
    /**
     * viewpager横向滑动的阈值
     */
    private static final int THRESHOLD_X_VIEW_PAGER = 60;
    /**
     * listview竖向滑动的阈值
     */
    private static final int THRESHOLD_Y_LIST_VIEW = 20;

    @SuppressLint("ClickableViewAccessibility")
    private void fixSlideConflict() {
        // ViewPager滑动冲突解决
        mViewPager.setOnTouchListener((v, event) -> {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                // 记录点击到ViewPager时候，手指的X坐标
                mLastX = event.getX();
            }
            if (action == MotionEvent.ACTION_MOVE) {
                // 超过阈值，禁止SwipeRefreshLayout下拉刷新，禁止ScrollView截断点击事件
                if (Math.abs(event.getX() - mLastX) > THRESHOLD_X_VIEW_PAGER) {
                    mRefreshLayout.setEnabled(false);
                    mScrollView.requestDisallowInterceptTouchEvent(true);
                }
            }
            // 用户抬起手指，恢复父布局状态
            if (action == MotionEvent.ACTION_UP) {
                mRefreshLayout.setEnabled(true);
                mRefreshLayout.requestDisallowInterceptTouchEvent(false);
            }
            return false;
        });

        // ListView滑动冲突解决
        mListView.setOnTouchListener((v, event) -> {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                mLastY = event.getY();
            }
            if (action == MotionEvent.ACTION_MOVE) {
                int top = mListView.getChildAt(0).getTop();
                float nowY = event.getY();
//                if (!isSvToBottom){
//                    // 允许scrollview拦截点击事件, scrollView滑动
//                    mScrollView.requestDisallowInterceptTouchEvent(false);
//                } else if (top==0&&nowY-mLastY>THRESHOLD_Y_LIST_VIEW){
//                    // 允许scrollview拦截点击事件， listView滑动
//                    mScrollView.requestDisallowInterceptTouchEvent(false);
//                } else {
//                    // 不允许scrollview拦截点击事件， listView滑动
//                    mScrollView.requestDisallowInterceptTouchEvent(true);
//
//                }

                if (!isSvToBottom) {
                    // 允许scrollview拦截点击事件, scrollView滑动
                    mScrollView.requestDisallowInterceptTouchEvent(false);
                } else {
                    if (top == 0 && nowY - mLastY > THRESHOLD_Y_LIST_VIEW) {
                        // 允许scrollview拦截点击事件, scrollView滑动
                        mScrollView.requestDisallowInterceptTouchEvent(false);
                    } else {
                        // 不允许scrollview拦截点击事件， listView滑动
                        mScrollView.requestDisallowInterceptTouchEvent(false);
                    }
                }
            }
            return false;
        });

//        mScrollView.setOnTouchListener((v, eve nt) -> {
//            int action = event.getAction();
//            if (action == MotionEvent.ACTION_DOWN) {
//                Logger.d("执行=ACTION_DOWN");
//            }
//            if (action == MotionEvent.ACTION_MOVE) {
//                Logger.d("执行=ACTION_MOVE");
//            }
//            if (action == MotionEvent.ACTION_UP) {
//                Logger.d("执行=ACTION_UP");
//            }
//
//            return false;
//        });
    }

    private List<String> getListViewData() {
        List<String> data = new ArrayList<>();
        for (int i = 1; i <= 20; i++) {
            data.add(i + " item");
        }
        return data;
    }

    private List<String> getViewPagerData() {
        List<String> data = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            data.add("page " + i);
        }
        return data;
    }

}
